//
//  QDStationTimeInfoHeaderView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/3.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit


let QDStationTimeCollectionViewHeight = 90

/// 地铁详情路径
final class QDStationTimeCollectionView: NWCollectionViewCell {

    /// 地铁详情的时间
    class QDTimeStationView: UIView {
        
        /// 站名
        var stationName:String = "" {
            willSet {
                
                let string = "\(newValue) 方向"
                let attribute = NSMutableAttributedString(string: string, attributes: [.font : Font.regular.font(size: 13),
                                                                                       .foregroundColor: QDColors.black])
                let range = (string as NSString).range(of: newValue)
                attribute.addAttribute(.foregroundColor, value: QDColors.theme, range: range)
                attribute.addAttribute(.font, value: Font.regular.font(size: 13), range: range)
                titleLabel.attributedText = attribute
            }
        }
        
        /// 显示图标
        let imageView = UIImageView()
        /// 显示站名称
        let titleLabel = UILabel()
        /// 显示时间的标签
        let timeLabel = UILabel()
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            
            imageView.clipsToBounds = true
            imageView.layer.cornerRadius = 5
            
            timeLabel.textColor = QDColors.separator
            timeLabel.font = Font.regular.font(size: 13)
            timeLabel.text = "首班车：88:88 - 末班车：88:88"
            timeLabel.textAlignment = .right
            
            addSubview(imageView)
            addSubview(titleLabel)
            addSubview(timeLabel)
            
            imageView.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.width.height.equalTo(10)
                make.left.equalToSuperview()
            }
            
            titleLabel.snp.makeConstraints { (make) in
                make.left.equalTo(imageView.snp.right).offset(5)
                make.height.equalTo(20)
                make.centerY.equalToSuperview()
            }
            
            timeLabel.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.right.equalToSuperview()
                make.height.equalTo(20)
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    
    
    /// 显示线路标号
    let titleLabel = UILabel()
    /// 去的方向
    let comeView = QDTimeStationView()
    /// 返回方向
    let backView = QDTimeStationView()
    
    override func buildView() {
        super.buildView()
        
        titleLabel.text = "13号线"
        titleLabel.font = Font.regular.font(size: 13)
        titleLabel.textColor = QDColors.black
        
        comeView.imageView.image = "metro_start".namedImage
        backView.imageView.image = "metro_end".namedImage
        
        contentView.addSubview(titleLabel)
        contentView.addSubview(comeView)
        contentView.addSubview(backView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(20)
        }
        
        comeView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(20)
            make.right.equalToSuperview().inset(15)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
        }
        
        backView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(20)
            make.right.equalToSuperview().inset(15)
            make.top.equalTo(comeView.snp.bottom).offset(10)
        }
        
        comeView.stationName = "董家口火车站"
        backView.stationName = "董家口火车站"
    }
}


/// 顶部放置路径地铁新线
final class QDStationTimeInfoHeaderView: UIView {
    
    /// 当前高度
    func height(count: NSInteger) -> CGFloat {
        return CGFloat(QDStationTimeCollectionViewHeight * count + 10 * (count - 1) + 20) + 0.5
    }
    
    /// 显示的站点时间信息
    var stationLines = [QDNearMetroStation]() {
        didSet {
            collectionView.reloadData()
        }
    }

    /// 布局对象
    lazy private var flowLayout: UICollectionViewFlowLayout = {
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: UIScreen.main.ritl_width, height: CGFloat(QDStationTimeCollectionViewHeight))
        flowLayout.minimumLineSpacing = 10
        flowLayout.minimumInteritemSpacing = .leastNormalMagnitude
        flowLayout.sectionInset.top = 10
        flowLayout.sectionInset.bottom = 10
        
        return flowLayout
    }()
    
    /// 集合视图
    lazy private var collectionView: UICollectionView = {
       return UICollectionView(frame: .zero, collectionViewLayout: self.flowLayout)
    }()
    
    /// 底部横线
    private let bottomView = UIView()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        collectionView.register(QDStationTimeCollectionView.self, forCellWithReuseIdentifier: "cell")
        collectionView.dataSource = self
        collectionView.isScrollEnabled = false//禁止滑动
        collectionView.backgroundColor = .white
        
        bottomView.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
        
        addSubview(collectionView)
        addSubview(bottomView)
        
        collectionView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.bottom.equalToSuperview().inset(0.5)
        }
        
        
        bottomView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(0.5)
            make.right.equalToSuperview().inset(0.5).priority(.high)
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension QDStationTimeInfoHeaderView: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return stationLines.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        
        if let timeCell = cell as? QDStationTimeCollectionView {
            let stationLine = stationLines[indexPath.row]
            
            timeCell.titleLabel.text = stationLine.lineName
            
            timeCell.comeView.stationName = stationLine.startStation
            timeCell.comeView.timeLabel.text = "首班车：\(stationLine.startFirst) - 末班车：\(stationLine.startLast)"
            
            timeCell.backView.stationName = stationLine.endStation
            timeCell.backView.timeLabel.text = "首班车：\(stationLine.endFirst) - 末班车：\(stationLine.endLast)"
        }
        
        return cell
    }
}
